/**
 *
 */
package demo.tellurium.module.cqp

import org.telluriumsource.ui.object.UrlLink;

import demo.tellurium.framework.BaseUIModule 


/**
 * @author roger.xu
 *
 */
class CQPAllDocumentsModule extends BaseUIModule {

    private static final String MODULE_NAME = "CQPAllDocuments";
    public static final String PATH_KEY = "cqp.alldocuments.path";

    @Override
    public String getModuleName() {
        return MODULE_NAME;
    }

    @Override
    public void defineUi() {
        ui.Container(uid: "CQPAllDocuments") {
            // SearchBox
            Container(uid: "SearchBox") {
                Container(uid: "AdvancedSearchButton", clocator: [id: "advancedSearchBtn"]) {
                    UrlLink(uid: "Link", clocator: [tag: "a"])
                }
                InputBox(uid: "SearchByName", clocator: [tag: "input", id: "documentName"])
                InputBox(uid: "SearchByOwner", clocator: [tag: "input", id: "documentOwner"])
                DOJO_ComboBox(uid: "DivisionComboBox", widgetid: "divisionList")
                DOJO_ComboBox(uid: "StatusComboBox", widgetid: "statusList")
                DOJO_ComboBox(uid: "AssessmentTypeComboBox", widgetid: "assessmentTypeList")
                DOJO_ComboBox(uid: "ShowByComboBox", widgetid: "showbyList")
                DOJO_ComboBox(uid: "GroupByComboBox", widgetid: "groupbyList")
                DOJO_ComboBox(uid: "SortByComboBox", widgetid: "sortbyList")
                Container(uid: "SearchButton", clocator: [tag: "div", id: "searchBtn"]) {
                    UrlLink(uid: "Link", clocator: [tag: "a"])
                }
                Container(uid: "ClearResultButton", clocator: [tag: "div", id: "resetBtn"]) {
                    UrlLink(uid: "Link", clocator: [tag: "a"])
                }
            }
            // Toolbar
            Container(uid: "Toolbar", clocator: [id: "toolbar"]) {
                Container(uid: "CreateDropDownButton") {
                    Button(uid: "CreateButton", clocator: [tag: "span", widgetid: "createBtn"], respond: ["mouseDown", "mouseEnter", "mouseLeave"])
                    Container(uid: "ListPopUp", clocator: [tag: "div", dijitpopupparent: "createBtn"]) {
                        Container(uid: "Menu", clocator: [tag: "table", id: "editMenu"]) {
                            List(uid: "MenuItems", clocator: [tag: "tbody"], separator: "tr") {
                                 UrlLink(uid: "{all}", respond: ["click", "mouseEnter", "mouseLeave"])
                             }
                        }
                    }
                }
                DOJO_Button(uid: "OpenButton", clocator: [tag: "span", widgetid: "openButton"])
                DOJO_Button(uid: "DeleteButton", clocator: [tag: "span", widgetid: "deleteButton"])
                DOJO_Button(uid: "ReportButton", clocator: [tag: "span", widgetid: "reportButton"])
            }
            // Info Dialog
            Container(uid: "InfoDialog", clocator: [id: "infoDlg"]) {
                Button(uid: "OKButton", clocator: [tag: "input", type: "button", id: "infoDlg_OK_btn"])
            }
            // ReportDialog
            Container(uid: "ReportDialog", clocator: [id: "reportDialog"]) {
                Button(uid: "OKButton", clocator: [tag: "input", type: "button", id: "reportDialog_ok_btn"])
                Button(uid: "CancelButton", clocator: [tag: "input", type: "button", id: "reportDialog_cancel_btn"])
            }
            // DeleteDialog
            Container(uid: "DeleteDialog", clocator: [id: "confirmListDlg"]) {
                Button(uid: "OKButton", clocator: [tag: "input", type: "button", id: "confirmDlgButtonSubmit"])
                Button(uid: "CancelButton", clocator: [tag: "input", type: "button", id: "confirmDlgButtonCancel"])
            }
            // TreeGrid
            TextBox(uid: "SearchResults", clocator: [id: "searchResult"])
            TextBox(uid: "TotalResults", clocator: [id: "totalAmounts"])
            Container(uid: "TreeGrid", clocator: [id: "treegrid"]) {
                Container(uid: "GridContent", clocator: [tag: "div", class: "dojoxGridContent"]) {
                    List(uid: "GridRows", clocator: [tag: "div", role: "presentation"], separator: "div") {
                        Container(uid: "{all}", clocator: [tag: "table", class: "dojoxGridRowTable"]) {
                            Container(uid: "GridCell-DocumentName", clocator: [tag: "td", class: "dojoxGridExpandoCell"]) {
                                Div(uid: "ExpandoNode", clocator: [class: "dojoxGridExpandoNode"])
                                CheckBox(uid: "CheckBox", clocator: [:])
                                UrlLink(uid: "DocumentLink", clocator: [tag: "a"])
                            }
                        }
                    }
                }
            }
        }
    }




    /* ---- SearchBox ---------------*/
    public void doClickAdvancedSearch() {
        click "CQPAllDocuments.SearchBox.AdvancedSearchButton.Link"
        pause 500
    }

    public void doSearchByName(String input) {
        type "CQPAllDocuments.SearchBox.SearchByName", input
        pause 500
        click "CQPAllDocuments.SearchBox.SearchButton.Link"
        waitForPageToLoad 30000
    }

    public void doInputSearchName(String input) {
        type "CQPAllDocuments.SearchBox.SearchByName", input
        pause 500
    }

    public void doInputSearchOwner(String input) {
        type "CQPAllDocuments.SearchBox.SearchByOwner", input
        pause 500
    }

    public void doSelectDivision(int index) {
        def widgetUid = "CQPAllDocuments.SearchBox.DivisionComboBox";
        doSelectComboBoxItem(widgetUid, index);
        pause 500
    }

    public void doSelectStatus(int index) {
        def widgetUid = "CQPAllDocuments.SearchBox.StatusComboBox";
        doSelectComboBoxItem(widgetUid, index);
        pause 500
    }

    public void doSelectAssessmentType(int index) {
        def widgetName = "AssessmentTypeComboBox";
        def widgetUid = String.format("%s.%s.%s", getModuleName(), "SearchBox", widgetName);
        doSelectComboBoxItem(widgetUid, index);
    }

    public void doShowBy(int index) {
        def widgetName = "ShowByComboBox";
        def widgetUid = String.format("%s.%s.%s", getModuleName(), "SearchBox", widgetName);
        doSelectComboBoxItem(widgetUid, index);
        pause 2000
    }

    public void doGroupBy(int index) {
        def widgetName = "GroupByComboBox";
        def widgetUid = String.format("%s.%s.%s", getModuleName(), "SearchBox", widgetName);
        doSelectComboBoxItem(widgetUid, index);
        pause 2000
    }

    public void doSortBy(int index) {
        def widgetName = "SortByComboBox";
        def widgetUid = String.format("%s.%s.%s", getModuleName(), "SearchBox", widgetName);
        doSelectComboBoxItem(widgetUid, index);
        pause 2000
    }

    public void doSearch() {
        click "CQPAllDocuments.SearchBox.SearchButton.Link"
        waitForPageToLoad 30000
    }

    public void doClearResult() {
        click "CQPAllDocuments.SearchBox.ClearResultButton.Link"
        waitForPageToLoad 30000
    }

    public int getSearchResults() {
        int searchResults = 0;
        String value = getText("CQPAllDocuments.SearchResults");
        searchResults = value.toInteger();

        return searchResults;
    }

    /* ---- Toolbar ---------------*/
    public void doClickCreateAssessmentButton() {
        doClickCreateButton();

        def toolbarUid = String.format("%s.Toolbar", getModuleName());
        def widgetUid = String.format("%s.%s", toolbarUid, "CreateDropDownButton");

        // expand menu
        def listUid = String.format("%s.ListPopUp.Menu.MenuItems", widgetUid);
        waitForElementPresent listUid, 5000

        // mouse over menu item
        def menuItemUid = String.format("%s[%d]", listUid, 1);
        mouseOver menuItemUid
        pause 1000

        // click menu item
        click menuItemUid
        waitForPageToLoad 30000
    }

    public void doClickCreatePreAssessmentButton() {
        doClickCreateButton();

        def toolbarUid = String.format("%s.Toolbar", getModuleName());
        def widgetUid = String.format("%s.%s", toolbarUid, "CreateDropDownButton");

        // expand menu
        def listUid = String.format("%s.ListPopUp.Menu.MenuItems", widgetUid);
        waitForElementPresent listUid, 5000

        // mouse over menu item
        def menuItemUid = String.format("%s[%d]", listUid, 3);
        mouseOver menuItemUid
        pause 1000

        // click menu item
        click menuItemUid
        waitForPageToLoad 30000
    }

    public void doClickCreateButton() {
        def toolbarUid = String.format("%s.Toolbar", getModuleName());
        def widgetUid = String.format("%s.%s", toolbarUid, "CreateDropDownButton");

        def createButtonUid = String.format("%s.CreateButton", widgetUid);
        click createButtonUid
        pause 500
    }

    public void doClickOpenButton() {
        def toolbarUid = String.format("%s.Toolbar", getModuleName());
        def widgetUid = String.format("%s.%s", toolbarUid, "OpenButton");
        doClickButton(widgetUid);
        waitForPageToLoad 30000
    }

    public void doClickDeleteButton() {
        def toolbarUid = String.format("%s.Toolbar", getModuleName());
        def widgetUid = String.format("%s.%s", toolbarUid, "DeleteButton");
        doClickButton(widgetUid);

        def deleteDialog = String.format("%s.DeleteDialog", getModuleName());
        waitForElementPresent deleteDialog, 10000
    }

    public void doClickReportButton() {
        def toolbarUid = String.format("%s.Toolbar", getModuleName());
        def widgetUid = String.format("%s.%s", toolbarUid, "ReportButton");
        doClickButton(widgetUid);
        waitForPageToLoad 30000
    }

    /* ---- Grid ---------------*/
    public void doClickCheckBox(int index) {
        def rowsUid = String.format("%s.TreeGrid.GridContent.GridRows", MODULE_NAME);
        def checkboxUid = String.format("%s[%d].GridCell-DocumentName.CheckBox", rowsUid, index);
        click checkboxUid
        pause 200
    }

    public void doClickCheckBoxes(int[] indexes) {
        if (indexes == null) {
            return;
        }

        def rowsUid = String.format("%s.TreeGrid.GridContent.GridRows", MODULE_NAME);
        int listSize = getListSize(rowsUid);
        console.debug(listSize);

        for (def index in indexes) {
            doClickCheckBox(index);
        }
    }

    public void doClickDocument(int index) {
        def rowsUid = String.format("%s.TreeGrid.GridContent.GridRows", MODULE_NAME);
        def linkUid = String.format("%s[%d].GridCell-DocumentName.DocumentLink", rowsUid, index);
        click linkUid
        pause 500
        waitForPageToLoad 30000
    }

    public void doExpandCell(int index) {
        def rowsUid = String.format("%s.TreeGrid.GridContent.GridRows", MODULE_NAME);
        def expandoUid = String.format("%s[%d].GridCell-DocumentName.ExpandoNode", rowsUid, index);
        click expandoUid
        pause 500
        waitForPageToLoad 30000
    }

    /* ---- Info Dialog ---------------*/
    public void doClickInfoDialogOKButton() {
        def uid = "CQPAllDocuments.InfoDialog.OKButton";
        click uid
        pause 500
    }

    /* ---- Delete ---------------*/
    public void doClickDeleteDialogOKButton() {
        def uid = "CQPAllDocuments.DeleteDialog.OKButton";
        click uid
        pause 500
        waitForPageToLoad 30000
    }

    public void doClickDeleteDialogCancelButton() {
        def uid = "CQPAllDocuments.DeleteDialog.CancelButton";
        click uid
        pause 500
    }

    /* ---- Report ---------------*/
    public void doClickReportDialogOKButton() {
        def uid = "CQPAllDocuments.ReportDialog.OKButton";
        click uid
        pause 500
        waitForPageToLoad 30000
    }

    public void doClickReportDialogCancelButton() {
        def uid = "CQPAllDocuments.ReportDialog.CancelButton";
        click uid
        pause 500
    }
}
